# -*- coding:utf8 -*-
#!/usr/bin/env python

import nltk, re
import numpy as np
from django.utils.encoding import smart_str, smart_unicode
from scikits.learn.svm import SVC

from django.core.management.base import BaseCommand, CommandError
from feedsaver.saver.models import *

def mountY():
	# Array que conterá o numero da classe
	arrayY = []

	vfcount = VectorFeatures.objects.filter(type='brasil').count()
	for i in range(0,vfcount/2):
		# Brasil - 1
		arrayY.append(1)

	vfcount = VectorFeatures.objects.filter(type='mundo').count()
	for i in range(0,vfcount/2):
		# Mundo - 2
		arrayY.append(2)

	vfcount = VectorFeatures.objects.filter(type='economia').count()
	for i in range(0,vfcount/2):
		# Economia - 3
		arrayY.append(3)

	vfcount = VectorFeatures.objects.filter(type='tecnologia').count()
	for i in range(0,vfcount/2):
		# Tecnologia - 4
		arrayY.append(4)

	vfcount = VectorFeatures.objects.filter(type='ciencia').count()
	for i in range(0,vfcount/2):
		# Ciencia - 4
		arrayY.append(4)

	vfcount = VectorFeatures.objects.filter(type='entretenimento').count()
	for i in range(0,vfcount/2):
		# entretenimento - 5
		arrayY.append(5)

	return arrayY

def mountX():
	# Array em cada indice corresponde a um vetor de caracteristicas
	arrayX = []

	vf = VectorFeatures.objects.filter(type='brasil')
	vfcount = VectorFeatures.objects.filter(type='brasil').count()

	for i in vf[:vfcount/2]:
		arrayX.append(i.values.split(','))

	vf = VectorFeatures.objects.filter(type='mundo')
	vfcount = VectorFeatures.objects.filter(type='mundo').count()

	for i in vf[:vfcount/2]:
		arrayX.append(i.values.split(','))

	vf = VectorFeatures.objects.filter(type='economia')
	vfcount = VectorFeatures.objects.filter(type='economia').count()

	for i in vf[:vfcount/2]:
		arrayX.append(i.values.split(','))

	vf = VectorFeatures.objects.filter(type='tecnologia')
	vfcount = VectorFeatures.objects.filter(type='tecnologia').count()

	for i in vf[:vfcount/2]:
		arrayX.append(i.values.split(','))

	vf = VectorFeatures.objects.filter(type='ciencia')
	vfcount = VectorFeatures.objects.filter(type='ciencia').count()

	for i in vf[:vfcount/2]:
		arrayX.append(i.values.split(','))

	vf = VectorFeatures.objects.filter(type='entretenimento')
	vfcount = VectorFeatures.objects.filter(type='entretenimento').count()

	for i in vf[:vfcount/2]:
		arrayX.append(i.values.split(','))

	return arrayX

class Command(BaseCommand):
    args = 'no args'
    help = 'Extract info from CPR'
    
    def handle(self, *args, **options):

       	arrayY = np.array(mountY())

       	arrayX = np.array(mountX())

       	clf = SVC()

       	clf.fit(arrayX, arrayY)

       	vfs = VectorFeatures.objects.all()
       	vfscount = VectorFeatures.objects.all().count()

       	total = 0
       	acerto = 0
       	for vf in vfs[vfscount/2:]:
       		total = total + 1
       		pred = clf.predict(vf.values.split(','))


       		if (int(pred[0]) ==  1 and vf.type == 'brasil'):
       			acerto = acerto+1
       			self.stdout.write("acertou brasil\n")

       		if (int(pred[0]) ==  2 and vf.type == 'mundo'):
       			acerto = acerto+1
       			self.stdout.write("acertou mundo\n")

       		if (int(pred[0]) ==  3 and vf.type == 'economia'):
       			acerto = acerto+1
       			self.stdout.write("acertou economia\n")
       		
       		if (int(pred[0]) ==  4 and vf.type == 'tecnologia'):
       			acerto = acerto+1
       			self.stdout.write("acertou tecnologia\n")

       		if (int(pred[0]) ==  5 and vf.type == 'ciencia'):
       			acerto = acerto+1
       			self.stdout.write("acertou ciencia\n")

       		if (int(pred[0]) ==  5 and vf.type == 'entretenimento'):
       			acerto = acerto+1
       			self.stdout.write("acertou entretenimento\n")

       	self.stdout.write("Acerto: "+str(acerto)+"\n")
       	self.stdout.write("Total: "+str(total)+"\n")

       	#vf = VectorFeatures.objects.filter(type='brasil')[1]

       	#self.stdout.write(str( clf.predict(vf.values.split(',') ) ))
       	#self.stdout.write('\n')
       	#self.stdout.write(vf.type)
       	#self.stdout.write('\n')

        return